/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.listener;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

import de.renier.jkeepass.actions.ActionManager;


/**
 * ElementSelectionListener
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class ElementSelectionListener implements TreeSelectionListener {

  /* (non-Javadoc)
   * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
   */
  public void valueChanged(TreeSelectionEvent e) {
    // show correct panel on right side
    TreePath treePath = e.getNewLeadSelectionPath();
    if (treePath != null) {
      // enable disable fields
      ActionManager.getCreateContainerAction().setEnabled(true);
      ActionManager.getCreateBasicElementAction().setEnabled(true);
      ActionManager.getCreateTanElementAction().setEnabled(true);
      
      // if not root
      if (treePath.getPathCount() > 1) {
        ActionManager.getDeleteContainerElementAction().setEnabled(true);
        ActionManager.getExportContainerAction().setEnabled(true);
      } else {
        ActionManager.getDeleteContainerElementAction().setEnabled(false);
        ActionManager.getExportContainerAction().setEnabled(false);
      }
    } else {
      // enable disable fields
      ActionManager.getCreateContainerAction().setEnabled(false);
      ActionManager.getCreateBasicElementAction().setEnabled(false);
      ActionManager.getDeleteContainerElementAction().setEnabled(false);
      ActionManager.getCreateTanElementAction().setEnabled(false);
      ActionManager.getExportContainerAction().setEnabled(false);
    }
  }

}
